<template>
	<view class="container">
		<view class="yy-date-head">
			<view class="yy-date-head-l">
				<view class="yy-date-head-lImg">
					<image :src="doc.avatar" mode="widthFix"></image>
				</view>
				<view class="yy-date-head-lExplain">
					<text class="yy-date-head-lE-name">{{doc.nickname}}</text>
					<view class="yy-date-head-lE-zw">
						<text>{{doc.department_name}}</text>
						<text>{{doc.title}}</text>
					</view>
				</view>
			</view>
			<view class="yy-date-head-r" @click="docdetail(docId)">
				<text>医生简介</text>
				<view class="yy-date-head-rImg">
					<image src="../../static/icon_right.png" mode="widthFix"></image>
				</view>
			</view>
		</view>
		
		
		<view class="yy-date-con">
			<view class="yy-mz-head">
				<view class="yy-mz-headItem"
				@click="changedata(index,item.dateAll.yearDate,item.disabled)"
				v-for="(item,index) in datalist"
				:key="index"
				:class="[currnum === index?'yy-mz-headShow':'',item.disabled === false ? 'yy-mz-headShowop':'']">
					<text>{{item.dateAll.date}}</text>
					<text>{{item.dateAll.week}}</text>
				</view>
			</view>
			<view class="yy-date-con-day">
				<view class="yy-date-con-dayAm" @click="changeday(0)" :class="currday == 0?'yy-date-Sactive':''">上午({{docAMnum}})</view>
				<view class="yy-date-con-dayAm" @click="changeday(1)" :class="currday == 1?'yy-date-Sactive':''">下午({{docPMnum}})</view>
			</view>
			<view class="yy-date-con-huor">
				<view class="yy-date-con-huor-Am" v-show="currday == 0">
					<view class="yy-date-con-huor-AmItem" v-for="(item,index) in docAM" :key="index">
						<view>{{item.time_text}}</view>
						<view v-if="item.number != 0" class="yy-date-hour-item" @click="booking(item.time_text,item.id)">剩余{{item.number}}</view>
						<view v-else class="yy-date-hour-Sactive">已满</view>
					</view>
				</view>
				<view class="yy-date-con-huor-Am" v-show="currday == 1">
					<view class="yy-date-con-huor-AmItem"  v-for="(item,index) in docPM" :key="index">
						<view>{{item.time_text}}</view>
						<view v-if="item.number != 0" class="yy-date-hour-item" @click="booking(item.time_text,item.id)">剩余{{item.number}}</view>
						<view v-else class="yy-date-hour-Sactive">已满</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import app from '../../App.vue'
	export default{
		data(){
			return{
				currnum:0,  //预约日期
				currday:0,  //预约时间
				data:'',	//选择时间
				doc:'',
				docId:'',	//医生id
				docAM:[],
				docPM:[],
				docAMnum:'',
				docPMnum:'',
				// dataList:[],
				status:'',
				mzid:'',
				datalist:[]
			}
		},
		onLoad(options) {
			// let start = new Date()
			// let end = new Date()
			// this.getnetDate(start,end,40)
			this.status = options.status
			this.mzid = options.mzId
			this.docId = options.id
			this.gettimelist()
		},
		onShow() {
			
		},
		methods:{
			changedata(i,data,status){
				this.currday = 0
				this.currnum = i
				this.data = data
				if(status === false){
					uni.showToast({
						title: '该天无法预约',
						icon: 'none',
						mask: true
					})
					this.docList = []
				}else{
					let date = data
					this.getdocdetail(data)
				}
			},
			
			
			
			changeday(num){
				this.currday = num
				if(num == 0 && this.docAM.length == 0){
					uni.showToast({
						title: '暂无数据',
						icon: 'none',
						mask: true
					})
				}else if(num == 1 && this.docPM.length == 0){
					uni.showToast({
						title: '暂无数据',
						icon: 'none',
						mask: true
					})
				}
			},
			// getnetDate(startDate, endDate, day) {				//获取时间和周天
			//     var startDate = startDate;
			//     var endDate = endDate;
			//     endDate.setDate(startDate.getDate() + day);
			//     var dataArr = [];
			//     var weeks = ['日', '一', '二', '三', '四', '五', '六'];
			// 	// return
			//     while ((endDate.getTime() - startDate.getTime()) >= 0) {
			//       var year = startDate.getFullYear()
			//       var month = (startDate.getMonth() + 1).toString().length == 1 ? "0" + (startDate.getMonth() + 1).toString() : (startDate.getMonth() + 1);
			//       var day = (startDate.getDate()+1).toString().length == 1 ? '0' + (startDate.getDate()+1) : (startDate.getDate()+1);
			//       // var daay = startDate.getDate();
			//       var week = weeks[startDate.getDay()+1>6?0:startDate.getDay()+1];
			//       let ayr = {}
			//       ayr.date = month + '-' + day
			//       ayr.week = '周 ' + week
			//       ayr.yearDate = year + '-' + month + '-' + day
			//       // ayr.day = daay
			//       dataArr.push(ayr)
			//       startDate.setDate(startDate.getDate() + 1);
			//     }
			//     console.log(dataArr, "日期");
			// 	dataArr[0]
			// 	this.dataList = dataArr
			//     return dataArr
			// },
			
			gettimelist(){
				let that = this
				let data = {
					department_id:that.mzid,
				}
				app.post('api/user_scheduling/getDateList',data).then(res=>{
					if(res.code === 1){
						that.datalist = res.data
						// console(typeof res.data)
						var weeks = ['日', '一', '二', '三', '四', '五', '六'];
						let timelist = []
						for(var i=0;i< that.datalist.length ;i++){
							let arrdata = {}
							that.datalist[i].dateAll = {}
							let date1 = that.datalist[i].date
							// console.log(date1)
							let date = new Date(date1*1000)
							
							let year = date.getFullYear()
							
							let month = date.getMonth() + 1
							month = month<10 ? "0"+month:month
							
							let day = date.getDate()
							day = day < 10 ? '0' + day:day
							
							
							var week = weeks[date.getDay()>6?0:date.getDay()];
							
							arrdata.date = month + '.' + day
							arrdata.week = '周 ' + week
							arrdata.yearDate = year + '-' + month + '-' + day
							// arrdata.disabled = list[i].disabled
							that.datalist[i].dateAll = arrdata
						}
						this.getdocdetail()
					}else if(res.code == 0){
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}else if(res.code == 401){
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err=>{
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			},
			
			docdetail(id){				//医生详情页面
				let that = this
				uni.showLoading({
					title:'加载中',
					success() {
						setTimeout(function(){
							uni.hideLoading()
							uni.navigateTo({
								url:'/pages/yuyue/doc?id='+id+'&status='+that.status+'&mzId='+that.mzId
							})
						},1300)
					}
				})
			},
			getdocdetail(date){				//医生详情
				// console.log(this.datalist)
				let that = this,
				data = {
					user_id:that.docId,
					date:that.currnum == 0?that.datalist[0].dateAll.yearDate:date
				}
				app.post('api/user_scheduling/getSchedulingList',data).then(res=>{
					if(res.code == 1){
						console.log(res.data)
						that.doc = res.data.doctor
						that.docAM = res.data.list.am
						that.docPM = res.data.list.pm
						
						that.docAMnum = res.data.am_num
						that.docPMnum = res.data.pm_num
					}
				})
			},
			booking(time,id){
				let that = this
				console.log(that.datalist)
				let month = that.datalist[that.currnum].dateAll.date
				let year = that.datalist[that.currnum].dateAll.yearDate
				uni.showLoading({
					title:'加载中',
					success() {
						setTimeout(function(){
							uni.hideLoading()
							uni.navigateTo({
								url:'/pages/yuyue/detail?docId='+that.docId+'&time='+time+'&month='+month+'&year='+year+'&paibanId='+id
							})
						},1300)
					}
				})
			}
			
		}
	}
</script>

<style>
	@import url("../../common/yuyuechoosedate.css");
</style>
